package com.zd.service.post.impl;

import com.zd.domain.*;
import com.zd.mapper.*;
import com.zd.service.comment.CommentService;
import com.zd.service.post.PostService;
import com.zd.service.user.UserService;
import org.apache.catalina.mapper.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class PostServiceImpl implements PostService {
    @Autowired
    private PostMapper postMapper;

    @Autowired
    private PostImgsMapper postImgsMapper;

    @Autowired
    private PostLikeMapper postLikeMapper;

    @Autowired
    private CommentMapper commentMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private CommentService commentService;

    /**
     * 查询用户的所有贴子
     * @param userId
     * @return
     */
    @Override
    public List<Post> myPosts(Integer userId) {



        List<Post> posts = postMapper.myPosts(userId);

        if (posts != null && posts.size() != 0){
            for (Post post : posts) {

                //根据postId查询post的图片
                List<PostImgs> postImgs = postImgsMapper.postImgs(post.getPostId());
                if ((postImgs != null) && (postImgs.size() != 0)){
                    post.setPostImgsList(postImgs);
                }


                //根据postId查询post的点赞人
                List<PostLike> postLikes = postLikeMapper.postLikes(post.getPostId());

                if ((postLikes != null) && (postLikes.size() != 0)){
                    post.setPostLikes(postLikes);
                }

                //查询点赞数
                Integer likeNum = postLikeMapper.findNum(post.getPostId());
                post.setLikeNum(likeNum);

                //根据postId查询评论
                List<Comment> comments = commentMapper.comments(post.getPostId());
                if ((comments != null) && (comments.size() != 0)){
                    post.setComments(comments);
                }

                //查询评论数
                Integer commentNum = commentMapper.findNum(post.getPostId());
                post.setCommentNum(commentNum);

            }

        }

        return posts;
    }

    /**
     * 删除贴子(修改贴子状态)
     * @param postId
     * @return
     */
    @Override
    public int updatePostState(Integer postId) {
        return postMapper.updatePostState(postId);
    }

    /**
     * 查询全部正常的贴子
     * @return
     */
    @Override
    public List<Post> list() {

        List<Post> posts = postMapper.list();

        if (posts != null && posts.size() != 0){
            for (Post post : posts) {

                //根据postId查询post的图片
                List<PostImgs> postImgs = postImgsMapper.postImgs(post.getPostId());
                if ((postImgs != null) && (postImgs.size() != 0)){
                    post.setPostImgsList(postImgs);
                }


                //根据postId查询post的点赞人
                List<PostLike> postLikes = postLikeMapper.postLikes(post.getPostId());

                if ((postLikes != null) && (postLikes.size() != 0)){
                    post.setPostLikes(postLikes);
                }

                //查询点赞数
                Integer likeNum = postLikeMapper.findNum(post.getPostId());
                post.setLikeNum(likeNum);

                //根据postId查询评论
                List<Comment> comments = commentMapper.comments(post.getPostId());
                if ((comments != null) && (comments.size() != 0)){
                    post.setComments(comments);
                }

                //查询评论数
                Integer commentNum = commentMapper.findNum(post.getPostId());
                post.setCommentNum(commentNum);

            }

        }

        return posts;
    }

    /**
     * 添加贴子
     * @param post
     * @return
     */
    @Override
    public int savePost(Post post) {

        //先添加贴子基本信息
        int i = postMapper.savePost(post);

        return i;

    }

    /**
     * 查询贴子的相关信息（根据postId）
     * @param postId
     * @return
     */
    @Override
    public Post findPost(Integer postId) {

        Post post = postMapper.findPost(postId);

        if (post != null){

            //根据postId查询post的图片
            List<PostImgs> postImgs = postImgsMapper.postImgs(post.getPostId());
            if ((postImgs != null) && (postImgs.size() != 0)){
                post.setPostImgsList(postImgs);
            }

            //根据userId查发帖人的信息
            User user1 = userMapper.findByUserID(post.getUserId());
            if (user1 != null){
                post.setUser(user1);
            }

            //根据postId查询post的点赞人
            List<PostLike> postLikes = postLikeMapper.postLikes(post.getPostId());

            if ((postLikes != null) && (postLikes.size() != 0)){
                post.setPostLikes(postLikes);
            }

            //查询点赞数
            Integer likeNum = postLikeMapper.findNum(post.getPostId());
            post.setLikeNum(likeNum);

            //根据postId查询评论
            List<Comment> comments = commentService.comments(post.getPostId());
            if ((comments != null) && (comments.size() != 0)){

                //遍历comments，
                for (Comment comment : comments) {

                    //查询评论的用户信息
                    User user = userMapper.findByUserID(comment.getUserId());
                    user.setUserKey(null);
                    user.setUserPassword(null);

                    comment.setUser(user);

                }

                post.setComments(comments);
            }

            //查询评论数
            Integer commentNum = commentMapper.findNum(post.getPostId());
            post.setCommentNum(commentNum);


        }

        return post;
    }

    /**
     * 添加贴子和贴子对应的图片
     * @param post
     * @return
     */
    @Override
    public int savePostAndImgs(Post post) {
        return postMapper.savePostAndImgs(post);
    }


}
